package com.atguigu.gulimall.shares.service.impl;

import com.atguigu.common.utils.HttpClientConfig;
import com.atguigu.common.utils.R;
import com.atguigu.gulimall.shares.constants.SharesConstants;
import com.atguigu.gulimall.shares.dto.SharesDto;
import com.atguigu.gulimall.shares.service.SharesService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.utils.HttpClientUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author : 刘思盖
 * @Date : 2022/6/14 11:14
 */
@Service("sharesService")
@Slf4j
public class SharesServiceImpl implements SharesService {
    
    
    @Override
    public R getByCode(String code) {
//        String requestUrl = SharesConstants.GET_SHARES_URL+code;
//        String refer = SharesConstants.GET_SHARES_REFER;
//        try {
//            log.info("requestUrl："+requestUrl);
//            String response = HttpClientUtil.doGet(requestUrl,"",refer);
//            List<SharesDto> list = buildResult(response);
//            Map<String,Object> map = new HashMap<>();
//            map.put("data",list);
//            return R.ok(map);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
        return R.error();
    }

    /**
     * 设置结果
     * @param result
     */
    private List<SharesDto> buildResult(String result){
        List<SharesDto> list = new ArrayList<>();
        if(StringUtils.isNotEmpty(result)){
            String[] sharesArr = result.split(";");
            for (String str : sharesArr) {
                String item = str.replaceAll("\\s*|\r|\n|\t","");
                if(StringUtils.isEmpty(item)){
                    break;
                }
                String[] arr = item.split(",");
                SharesDto sharesDto = new SharesDto();
                sharesDto.setSharesName(arr[0]);
                sharesDto.setTodayOpenPrice(arr[1]);
                sharesDto.setYesterdayClosePrice(arr[2]);
                sharesDto.setCurrentPrice(arr[3]);
                //  现价-上一个交易日收盘价)/上一个交易日收盘价*100%。
                BigDecimal subtractResult = new BigDecimal(sharesDto.getCurrentPrice()).subtract(new BigDecimal(sharesDto.getYesterdayClosePrice()));
                BigDecimal divideResult = subtractResult.divide(new BigDecimal(sharesDto.getYesterdayClosePrice()),4,BigDecimal.ROUND_HALF_UP);
                BigDecimal multiplyResult = divideResult.multiply(new BigDecimal("100"));
                sharesDto.setFluctuationRange(multiplyResult.toString());
                list.add(sharesDto);
            }
            return list;
        }
        return null;
    }

    public static void main(String[] args) {
        String bo = "false";
        boolean b = BooleanUtils.toBoolean(bo);
        System.out.println(b);
    }
}
